package com.aos.erp.common.utils;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2018/6/16.
 */
public class DateUtils {

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat xtimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
    private static SimpleDateFormat xxtimeFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");


    /**
     * 清除 时分秒,毫秒
     * @param calendar
     * @return
     */
    public static Calendar clearTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);

        return calendar;
    }

    public static String covertToDateString(long timeMillis) {
        if (timeMillis > 0) {
            return xtimeFormat.format(new Date(timeMillis));
        } else {
            return "";
        }
    }

    public static String getFormatTimeString(Date date, String format) {
        SimpleDateFormat toTimeFormat = new SimpleDateFormat(format);
        return toTimeFormat.format(date);
    }

    public static String getDateString(Date date) {
        return dateFormat.format(date);
    }
    public static String getTimeString(Date date) {
        return timeFormat.format(date);
    }
    public static String getXTimeString(Date date) {
        return xtimeFormat.format(date);
    }
    public static String getXXTimeString(Date date) {
        return xxtimeFormat.format(date);
    }


    public static String getDateString() {
        return getDateString(Calendar.getInstance().getTime());
    }
    public static String getTimeString() {
        return getTimeString(Calendar.getInstance().getTime());
    }
    public static String getXTimeString() {
        return getXTimeString(Calendar.getInstance().getTime());
    }
    public static String getXXTimeString() {
        return getXXTimeString(Calendar.getInstance().getTime());
    }


    /**
     * 字符串必须是年,月，日，时，分，秒，毫秒的顺序
     * @param str
     * @return
     */
    public static Date toDate(String str){
        String temp=str;
        //遍历字符串
        int[] values=new int[]{0,0,0,0,0,0,0};//年,月，日，时，分，秒，毫秒
        int vIndex=0;
        int l=temp.length();

        for(int i=0;i<l;) {
            char c = temp.charAt(i);
            //有效数字开头
            if ('0' <= c && c <= '9') {
                for (int j = i; j < l; j++) {
                    c = temp.charAt(j);
                    if (c > '9' || c < '0') {
                        //有效数字结束
                        String v = temp.substring(i, j);
                        values[vIndex++] = Integer.parseInt(v);
                        i = j;
                        break;
                    } else if (j >= l - 1) {
                        //字符串串完成
                        String v = temp.substring(i, j+ 1);
                        values[vIndex++] = Integer.parseInt(v);
                        i = j+1;
                        break;
                    }
                }
            }else{
                i++;
            }
        }




        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.YEAR,values[0]>100?values[0]:(values[0]+2000));//如果时2位，默认处理为2000年以后
        calendar.set(Calendar.MONTH,values[1]>0?(values[1]-1):0);//月份从0开始的，所以默认为0(1月)
        calendar.set(Calendar.DATE,values[2]>0?values[2]:1);//日期从1开始的，所以默认为1号

        calendar.set(Calendar.HOUR_OF_DAY,values[3]);
        calendar.set(Calendar.MINUTE,values[4]);
        calendar.set(Calendar.SECOND,values[5]);
        calendar.set(Calendar.MILLISECOND,values[6]);

        return calendar.getTime();
    }


    public static void main(String[] args){
        Date time=toDate(" 2018 -- 01 02 11 12 23 345 ");
        System.out.println(getXTimeString(time));
    }

}
